/**
 * 
 */
package jmine.tec.aom.domain.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.persistence.Column;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;

import jmine.tec.aom.AOMMessages;
import jmine.tec.aom.domain.AbstractPropertyValue;
import jmine.tec.aom.exception.FillException;
import jmine.tec.persist.annotation.DiscriminatorComment;

import org.hibernate.annotations.Type;

import bancosys.tec.component.Documentation;
import bancosys.tec.utils.date.Date;

/**
 * {@link AbstractPropertyValue} implementation for the {@link Date} type.
 * 
 * @author jrenaut
 */
@Entity
@DiscriminatorValue("3")
@DiscriminatorComment("DATE")
public class DatePropertyValue extends AbstractPropertyValue<Date> {

    /**
     * This is the date format used for {@link #setValue(String)} and {@link #getValueAsString()}
     */
    public static final String DATE_PROPERTY_FORMAT = "dd/MM/yyyy";

    /**
     * {@inheritDoc}
     */
    @Override
    @Column(name = "VL_DATA")
    @Documentation("VALOR DA CARACTERISTICA DO TIPO DATE")
    @Type(type = "bancosys.tec.persist.hibernate.type.DateType")
    public Date getValue() {
        return super.getValue();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void doSetValue(String value) throws FillException {
        try {
            this.setValue(new Date(new SimpleDateFormat(DATE_PROPERTY_FORMAT).parse(value)));
        } catch (ParseException e) {
            throw new FillException(AOMMessages.INVALID_DATE_FORMAT.create(this.getPropertyName(), value), e);
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected String doGetValueAsString() {
        return new SimpleDateFormat(DATE_PROPERTY_FORMAT).format(this.getValue());
    }

}
